package com.icbc.cc.client.util.http;

import java.util.Calendar;
import java.util.Map;

import org.json.JSONObject;
import org.json.JSONTokener;

import com.icbc.cc.security.app.AESUtil;
import com.icbc.cc.security.app.CommonHttpRequest;
import com.icbc.cc.security.app.CommonUtil;
import com.icbc.cc.security.app.DSAUtil;
import com.icbc.cc.security.app.LogUtil;
import com.icbc.cte.logging.Log;
import com.icbc.cte.logging.LogFactory;


public class ClientUtil {
	private String aesKey;
	private String dsaPri;

	private static Log log = LogFactory.getCtpLog(ClientUtil.class);
	
	public ClientUtil(String aesKey,String dsaPri){
		this.aesKey=aesKey;
		this.dsaPri=dsaPri;
	}
	/**
	 * 
	 * <pre>
	 * 璇锋眰鎺ュ彛   鍔犲瘑+绛惧悕锛堝鏈嶅姟绔繑鍥炵殑鏁版嵁涓嶅仛绛惧悕鏍￠獙锛
	 * </pre>
	 * @param commonURL 鎺ュ彛鍦板潃
	 * @param reqMethod 璇锋眰鏂瑰紡  POST/GET绛夌瓑銆傘�傘�
	 * @param platformNo 鍟嗘埛缂栧彿
	 * @param reqData  涓氬姟鏁版嵁锛坖son瀵硅薄瀛楃鍒涳級 娉ㄦ剰锛氫笉鍙互涓簄ull锛岃嫢鏃犲弬鏁版椂璇疯缃负new JSONObject().toString()鍗硔}
	 * @return
	 * @throws Exception
	 */
	public String call(String commonURL,String reqMethod,String platformNo,String bizid,String reqData) throws Exception{
		
		return call(commonURL, reqMethod, platformNo, bizid, reqData, true);
	}
	
	/**
	 * 
	 * <pre>
	 * 璇锋眰鎺ュ彛   鍔犲瘑+绛惧悕锛堝鏈嶅姟绔繑鍥炵殑鏁版嵁涓嶅仛绛惧悕鏍￠獙锛
	 * </pre>
	 * @param commonURL 鎺ュ彛鍦板潃
	 * @param reqMethod 璇锋眰鏂瑰紡  POST/GET绛夌瓑銆傘�傘�
	 * @param platformNo 鍟嗘埛缂栧彿
	 * @param reqData  涓氬姟鏁版嵁锛坖son瀵硅薄瀛楃鍒涳級 娉ㄦ剰锛氫笉鍙互涓簄ull锛岃嫢鏃犲弬鏁版椂璇疯缃负new JSONObject().toString()鍗硔}
	 * @param logFlg log璁板綍锛坱rue:璁板綍 false锛氫笉璁板綍锛
	 * @return
	 * @throws Exception
	 */
	public String call(String commonURL,String reqMethod,String platformNo,String bizid,String reqData, boolean logFlg) throws Exception{
		
		long time = Calendar.getInstance().getTimeInMillis();
		if (logFlg) {
			log.info("ClientUtil.call " + time + " commonURL:" + commonURL);
			log.info("ClientUtil.call " + time + " reqMethod:" + reqMethod);
			log.info("ClientUtil.call " + time + " platformNo:" + platformNo);
			log.info("ClientUtil.call " + time + " bizid:" + bizid);
			log.info("ClientUtil.call " + time + " reqData:" + reqData);
		}

		String reMsg=null;
		
		try {
			//1.瀵箁eqData杩涜AES鍔犲瘑绛惧悕 鏁版嵁鏁村悎
			String msg=CommonUtil.encryptAndSign(this.aesKey, this.dsaPri, reqData, platformNo,bizid);
			//2.鍙戣捣璇锋眰
			reMsg=CommonHttpRequest.doHttpRequest(commonURL, reqMethod, 300000, "jsonStr="+msg, "UTF-8");
			//3.瀵瑰搷搴旂殑鏁版嵁瑙ｅ瘑
			if(reMsg != null && reMsg.length() > 0){
				JSONObject re=new JSONObject(new JSONTokener(reMsg));
				String respData=re.getString("respData");
				if(respData != null && respData.length() > 0){
					String de=CommonUtil.decrypt(this.aesKey, respData);
					re.put("respData", de);
					reMsg=re.toString();
				}
			}
			
		} catch (Exception e) {
			log.error("ClientUtil.call " + time + " error:" + e);
			LogUtil.recordApiLog(commonURL, bizid, reqData, e.toString());
			return reMsg;
		} finally {
			if (reMsg == null) {
				reMsg = "{}";
			}
		}

		if (logFlg) {
			log.info("ClientUtil.call " + time + " reMsg:" + reMsg);
			LogUtil.recordApiLog(commonURL, bizid, reqData, reMsg);
		}

		return reMsg;
	}
	
	public String callWithSign(String commonURL,String reqMethod,String platformNo,String bizid,String reqData,String pubKey) throws Exception{
		String reMsg=null;
		//1.瀵箁eqData杩涜AES鍔犲瘑绛惧悕 鏁版嵁鏁村悎
		String msg=CommonUtil.encryptAndSign(this.aesKey, this.dsaPri, reqData, platformNo,bizid);
		//2.鍙戣捣璇锋眰
		reMsg=CommonHttpRequest.doHttpRequest(commonURL, reqMethod, 300000, "jsonStr="+msg, "UTF-8");
		//3.瀵瑰搷搴旂殑鏁版嵁瑙ｅ瘑
		if(reMsg != null && reMsg.length() > 0){
			JSONObject re=new JSONObject(new JSONTokener(reMsg));
			String respData=re.getString("respData");
			String sign=re.getString("sign");
			//绛惧悕鏍￠獙
			if(DSAUtil.verify(respData.getBytes(), pubKey, sign)){//绛惧悕閫氳繃
				if(respData != null && respData.length() > 0){
					String de=CommonUtil.decrypt(this.aesKey, respData);
					re.put("respData", de);
					reMsg=re.toString();
				}
			}else{
				
			}
			
		}
		return reMsg;
	}
	//鐢熸垚AES瀵嗛挜涓嶥SA鍏挜绉侀挜
	protected int execute() throws Exception{
		//鐢熸垚AES瀵嗛挜
		String randomStr16=CommonUtil.generateRandomStr(16);
		String aesKey=AESUtil.generateKey(randomStr16);
		//鐢熸垚鍟嗘埛鐨勮璇丏SA鍏挜涓庣閽
		String randomStr32= CommonUtil.generateUUID();
		Map<String, Object> map=DSAUtil.initKey(randomStr32);
		map.get(DSAUtil.PUBLIC_KEY);//DSA鍏挜
		map.get(DSAUtil.PRIVATE_KEY);//DSA绉侀挜
		return 0;
	}
}
